<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Adminuser extends MY_Controller {
    public function __construct(){
        parent::__construct();
        $this->sess_checked();
        $this->menuauth();
        $this->load->model('adminuser_m');
    }
    public function lists(){
        $where=array();
        $res=$this->adminuser_m->ajaxall($where);
        $this->assign('list',$res['list']);
        if(isset($_POST['ajaxs'])){
            $htmlcontent=$this->fetch('adminuser/adminuserlists.html');
            $res['list']=$htmlcontent;
            ajax_return(1,'',$res);
        }
        $this->assign('pageshow',$res['pageshow']);
        $this->display('adminuser/index.html');
    }
    //添加管理员
    public function add(){
        if(isset($_POST['ajaxs'])){//添加数据
            $data=form_ajaxs($_POST['formjquery']);
            $this->adminuser_data($data);
            if($data['password']!=$data['repassword']){
                ajax_return(0,'两次密码不相同');
            }
            //判断登陆账号是否已经存在
            $where['loginname']=array('=',$data['loginname']);
            if($this->adminuser_m->sel($where)){
                ajax_return(0,'登录名已经存在');
            }
            if($addid=$this->adminuser_m->add($data)){
                $this->adminuserRecord_m->add(1,1,'管理员id为'.$addid);
                ajax_return(1,'添加成功');
            }else{
                $this->adminuserRecord_m->add(1,2,'管理员');
                ajax_return(0,'添加失败');
            }
        }else{//添加页面
            $this->display('adminuser/add.html');
        }
    }
    //编辑管理员
    public function edit(){
        if(isset($_POST['ajaxs'])){//编辑数据
            $data=form_ajaxs($_POST['formjquery']);
            $where['id']=array('=',$data['id']);
            if($data['password']!=''){
                if(!cms_length($data['password'],6)  || !check_regex($data['password'],'isnozh') ){
                    ajax_return(0,'密码长度不能小于6个英文与数字组成的字符');
                }
                $data['[password']=passcreate($data['password']);
            }else{
                unset($data['password']);
            }
            if($this->adminuser_m->edit($where,$data)===false){
                $this->adminuserRecord_m->add(2,2,'管理员id为'.$data['id']);
                ajax_return(0,'提交失败');
            }else{
                $this->adminuserRecord_m->add(2,1,'管理员id为'.$data['id']);
                ajax_return(1,'提交成功');
            }
        }elseif(isset($_GET['id'])){
            $where['id']=array('=',$_GET['id']);
            $list=$this->adminuser_m->sel($where);
            $this->assign('list',$list);
            $this->display('adminuser/edit.html');
        }else{
            $this->error_msg('参数错误');
        }
    }
    //管理员权限设置
    public function role(){
        if(isset($_GET['id'])){
            $this->load->model('column_m');
            $data = $this->column_m->getAllColumnList(array('parentid'=>0));
            $data = $this->column_m->getClearColumnList($data, 0);
            $res = array();
            $sites =  array();
            foreach ($data as $k=>$v)
            {
                if(!in_array($v['sitesid'], $sites)){
                    $sites[] = $v['sitesid'];
                    $res[$v['sitesid']]['sitename'] = $v['sitename'];
                    $res[$v['sitesid']]['columns'][] = $v;
                }else{
                    $res[$v['sitesid']]['columns'][] = $v;
                }
            }
            $this->assign('allColumns',$res);
            $this->display('adminuser/role.html');
        }else{
            $this->error_msg('参数错误');
        }

    }
    protected function adminuser_data($data){
        if(!cms_length($data['loginname'],5,16) || !check_regex($data['loginname'],'isnozh')){
            ajax_return(0,'账号长度为5-15个英文与数字组成的字符');
        }
        if(!cms_length($data['password'],6)  || !check_regex($data['password'],'isnozh') ){
            ajax_return(0,'密码长度不能小于6个英文与数字组成的字符');
        }
    }

}
